#include "cpu.h"
#include "dev_flash.h"

void dev_flash_write(uint32_t offset_bytes_in_page, uint8_t *WriteBuf, int32_t WriteNum, uint32_t flash_address) 
{
	U32 d = 0;
  int i = 0;
	offset_bytes_in_page=(U32)flash_address + offset_bytes_in_page; 
	cpu_flash_unlock(); 
  cpu_flash_clear_flag();
	cpu_flash_erase_page(flash_address);
	while(i<WriteNum)
	{
	 d =(WriteBuf[i+3]<<24)|(WriteBuf[i+2]<<16)|(WriteBuf[i+1]<<8)|(WriteBuf[i+0]);
   cpu_flash_program_word(offset_bytes_in_page,d);
   i+=4;offset_bytes_in_page+=4;
	}
	cpu_flash_lock();	
}
void dev_flash_read(uint32_t offset_bytes_in_page, uint8_t *ReadBuf, int32_t ReadNum,uint32_t flash_address) 
{
	int i = 0;
	offset_bytes_in_page = (U32)flash_address + offset_bytes_in_page; 
	U8 *d = (U8*)offset_bytes_in_page;
  while(i<ReadNum){ *ReadBuf++ = *d++; i++;}
}
